package lab5p2;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import lab5p1.TrafficSignal;

public class TrafficSignalGUI extends JFrame implements ActionListener{
	public static void main(String[] args) {
		new TrafficSignalGUI();
	}

	public TrafficSignalGUI(){
		setBounds( X_POSITION, Y_POSITION, FRAME_WIDTH, FRAME_HEIGHT );
		setLayout( null );
		
		/*
		 * Set up labels
		 */
		ewLabel = new JLabel("East-West");
		ewLabel.setBounds(5, 30, 100, 30);
		add( ewLabel );
		
		nsLabel = new JLabel("North-South");
		nsLabel.setBounds(300, 30, 100, 30);
		add( nsLabel );

		/*
		 * Set up Button
		 */
		change = new JButton("Change");
		change.setBounds( BUTTON_X_POSITION, BUTTON_Y_POSITION, BUTTON_WIDTH, BUTTON_HEIGHT );
		change.addActionListener( this );
		add( change );
		
		/*
		 * Create traffic signals
		 */
		eastwest = new TrafficSignal();
		eastwest.change();
		eastwest.change();
		northsouth = new TrafficSignal();
		
		/*
		 * finalize setup
		 */
		setVisible( true );
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	/**
	 * actionPerformed - respond to user input
	 *    - button clicks
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		eastwest.change();
		northsouth.change();
		repaint();
	}

	public void paint(Graphics g){
		super.paint(g);
		final double WIDTH = 30.0, HEIGHT = 30.0, GAP = 10;
		final Color OFF = Color.DARK_GRAY;
		
		Graphics2D g2D = (Graphics2D) g;
		
		Color ewRed    = OFF;
		Color ewYellow = OFF;
		Color ewGreen  = OFF;
		Color nsRed    = OFF;
		Color nsYellow = OFF;
		Color nsGreen  = OFF;
		
		switch (eastwest.light()){
		case 0: ewGreen  = Color.green; break;
		case 1: ewYellow = Color.yellow; break;
		case 2: ewRed    = Color.red; break;
		default:
		}

		switch (northsouth.light()){
		case 0: nsGreen  = Color.green; break;
		case 1: nsYellow = Color.yellow; break;
		case 2: nsRed    = Color.red; break;
		default:
		}

		/*
		 * East - West lights
		 */
		double x = 10.0, y = 100.0;
		Ellipse2D.Double ewred  = new Ellipse2D.Double(x, y, WIDTH, HEIGHT);
		g2D.setColor(ewRed);
		g2D.fill(ewred);

		y += HEIGHT + GAP;
		Ellipse2D.Double ewyellow = new Ellipse2D.Double(x, y, WIDTH, HEIGHT);
		g2D.setColor(ewYellow);
		g2D.fill(ewyellow);
		
		y += HEIGHT + GAP;
		Ellipse2D.Double ewgreen  = new Ellipse2D.Double(x, y, WIDTH, HEIGHT);
		g2D.setColor(ewGreen);
		g2D.fill(ewgreen);

		/*
		 * North - South lights
		 */
		x = 300.0; y = 100.0;
		Ellipse2D.Double nsred  = new Ellipse2D.Double(x, y, WIDTH, HEIGHT);
		g2D.setColor(nsRed);
		g2D.fill(nsred);

		y += HEIGHT + GAP;
		Ellipse2D.Double nsyellow = new Ellipse2D.Double(x, y, WIDTH, HEIGHT);
		g2D.setColor(nsYellow);
		g2D.fill(nsyellow);
		
		y += HEIGHT + GAP;
		Ellipse2D.Double nsgreen  = new Ellipse2D.Double(x, y, WIDTH, HEIGHT);
		g2D.setColor(nsGreen);
		g2D.fill(nsgreen);

	}
	
	/*
	 * Constants
	 */
	private final int X_POSITION = 100,
			          Y_POSITION = 100,
			          FRAME_WIDTH = 400,
			          FRAME_HEIGHT = 300;
	private final int BUTTON_X_POSITION = 100,
					  BUTTON_Y_POSITION = 100,
					  BUTTON_WIDTH      = 100,
					  BUTTON_HEIGHT     = 50;
	private static final long serialVersionUID = 1L;
	
	/*
	 * Data members
	 */
	private JButton change;
	private JLabel  ewLabel, nsLabel;
	private TrafficSignal eastwest, northsouth;
}
